
package com.yuan.graduate;

import com.yuan.application.BaseActivity;
import com.yuan.control.SettingControl;
import com.yuan.dialog.DialogUtil;
import com.yuan.view.CheckBoxPreference;
import com.yuan.view.ListPreference;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * 系统设置界面
 * 
 * @author YUAN
 */
@SuppressLint("HandlerLeak")
public class MainSettingActivity extends BaseActivity {
    public static final String TAG = "MainSettingActivity";
    private Context mContext = null;
    private CheckBoxPreference mainsetting_shake = null, mainsetting_voice = null,
            mainsetting_autocheckin = null,
            mainsetting_autocheckout = null;
    private ListPreference mainsetting_start_time = null, mainsetting_end_time = null,
            mainsetting_adress = null,
            mainsetting_reset = null, mainsetting_week = null, mainsetting_lock = null;
    private Handler mHandler = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_mainsetting);
        initView();
        mHandler = new Handler() {
            public void handleMessage(Message msg) {// 此方法在ui线程运行
                switch (msg.what) {
                    case MAINSETTINGREFRESH:
                        initView();
                        break;
                }
            }
        };
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }

    private void initView() {
        // TODO Auto-generated method stub
        initShake();
        initVoice();
        initAutoCheckIn();
        initAutoCheckOut();
        initStartWorkTime();
        initEndWorkTime();
        initAddress();
        initReset();
        initWeek();
        initLock();
    }

    private void initLock() {
        // TODO Auto-generated method stub
        mainsetting_lock = (ListPreference) findViewById(R.id.mainsetting_lock);
        mainsetting_lock.setTitle("设置手势密码");
        mainsetting_lock.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                startActivity(new Intent(mContext, LockSetupActivity.class));
            }
        });
    }

    private void initWeek() {
        // TODO Auto-generated method stub
        mainsetting_week = (ListPreference) findViewById(R.id.mainsetting_week);
        mainsetting_week.setTitle("每周上班天数");
        mainsetting_week.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                DialogUtil.createWeekDialog(mContext).show();
            }
        });
    }

    private void initReset() {
        // TODO Auto-generated method stub
        mainsetting_reset = (ListPreference) findViewById(R.id.mainsetting_reset);
        mainsetting_reset.setTitle("恢复系统设置");
        mainsetting_reset.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                DialogUtil.createResetConfigDialog(mContext, "消息提示", "是否恢复系统设置？",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                SettingControl.RetConfig();
                                initView();
                                dialog.dismiss();
                            }
                        }).show();
            }
        });
    }

    private void initAddress() {
        // TODO Auto-generated method stub
        mainsetting_adress = (ListPreference) findViewById(R.id.mainsetting_adress);
        mainsetting_adress.setTitle("公司地点");
        mainsetting_adress.setKey(WEEK_ADRESS);
        mainsetting_adress.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(mContext, MapActivity.class));
            }
        });
    }

    /**
     * 设置上班打卡时间，通过设置setKey（String mString） 设置之后可以自动存储到配置文档中
     */
    private void initStartWorkTime() {
        // TODO Auto-generated method stub
        mainsetting_start_time = (ListPreference) findViewById(R.id.mainsetting_start_time);
        mainsetting_start_time.setTitle("上班时间:");
        mainsetting_start_time.setKey(START_WORK_TIME);
        mainsetting_start_time.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                DialogUtil.createStartWorkTimeInputDialog(mContext, mHandler).show();
            }
        });
    }

    private void initEndWorkTime() {
        // TODO Auto-generated method stub
        mainsetting_end_time = (ListPreference) findViewById(R.id.mainsetting_end_time);
        mainsetting_end_time.setTitle("下班时间:");
        mainsetting_end_time.setKey(END_WORK_TIME);
        mainsetting_end_time.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                DialogUtil.createEndWorkTimeInputDialog(mContext, mHandler).show();
            }
        });
    }

    private void initAutoCheckIn() {
        // TODO Auto-generated method stub
        mainsetting_autocheckin = (CheckBoxPreference) findViewById(R.id.mainsetting_autocheckin);
        mainsetting_autocheckin.setKey(AUTO_CHECKIN);
        mainsetting_autocheckin.setSummary("上班是否自动打卡");
    }

    private void initAutoCheckOut() {
        // TODO Auto-generated method stub
        mainsetting_autocheckout = (CheckBoxPreference) findViewById(R.id.mainsetting_autocheckout);
        mainsetting_autocheckout.setKey(AUTO_CHECKOUT);
        mainsetting_autocheckout.setSummary("下班是否自动打卡");
    }

    private void initVoice() {
        // TODO Auto-generated method stub
        mainsetting_voice = (CheckBoxPreference) findViewById(R.id.mainsetting_voice);
        mainsetting_voice.setKey(IS_VOLICE);
        mainsetting_voice.setSummary("打卡时是否带有声音");
    }

    private void initShake() {
        // TODO Auto-generated method stub
        mainsetting_shake = (CheckBoxPreference) findViewById(R.id.mainsetting_shake);
        mainsetting_shake.setKey(IS_SHARE);
        mainsetting_shake.setSummary("打卡时是否震动");
    }

    @Override
    public void onDestroy() {
        mContext = null;
        mainsetting_shake = null;
        mainsetting_voice = null;
        mainsetting_autocheckin = null;
        mainsetting_autocheckout = null;
        mainsetting_start_time = null;
        mainsetting_end_time = null;
        mainsetting_adress = null;
        mainsetting_reset = null;
        mainsetting_week = null;
        mHandler = null;
        super.onDestroy();
    }

    @Override
    public void onMyDestroy() {
        // TODO Auto-generated method stub
        mContext = null;
        mainsetting_shake = null;
        mainsetting_voice = null;
        mainsetting_autocheckin = null;
        mainsetting_autocheckout = null;
        mainsetting_start_time = null;
        mainsetting_end_time = null;
        mainsetting_adress = null;
        mainsetting_reset = null;
        mainsetting_week = null;
        mHandler = null;
        super.onMyDestroy();
    }
}
